Beispiel #1
0
        /// <summary>
        /// Check NameAddress record is the supplied name type ("C" for customer, "S" for supplier, "O" for other)
        /// </summary>
        protected void checkNameType(int?id, string allowed)
        {
            Utils.Check(id != null, allowed.NameType() + " missing");
            NameAddress n = Database.Get <NameAddress>((int)id);

            checkNameType(n.Type, allowed);
        }
Beispiel #2
0
        public void Name(int id)
        {
            // When maintaining names, use the template in banking
            Module = "banking";
            NameAddress record = Database.Get <NameAddress>(id);

            // Can only maintain Other names here
            if (record.Id == null)
            {
                record.Type = "O";
            }
            else
            {
                checkNameType(record.Type, "O");
                Title += " - " + record.Name;
            }
            Record = record;
        }
Beispiel #3
0
        public AjaxReturn Email(int id)
        {
            Extended_Document header   = prepareInvoice(id);
            NameAddress       customer = Database.Get <NameAddress>((int)header.DocumentNameAddressId);

            Utils.Check(!string.IsNullOrEmpty(Settings.CompanyEmail), "Company has no email address");
            Utils.Check(!string.IsNullOrEmpty(customer.Email), "Customer has no email address");
            Utils.Check(customer.Email.Contains('@'), "Customer has an invalid email address");
            ((JObject)((JObject)Record)["header"])["doctype"] = header.DocType.ToLower();
            ((JObject)Record)["customer"] = customer.ToJToken();
            string text    = LoadTemplate("customer/email.txt", this);
            string subject = Utils.NextToken(ref text, "\n").Trim();

            using (MemoryStream stream = new MemoryStream(Encoding.GetBytes(LoadTemplate("customer/print", this)))) {
                // Create a message and set up the recipients.
                MailMessage message = new MailMessage();
                message.From = new MailAddress(Settings.CompanyEmail);
                foreach (string e in customer.Email.Split(','))
                {
                    message.To.Add(e);
                }
                message.Bcc.Add(Settings.CompanyEmail);
                message.Subject = subject;
                message.Body    = text;
                // Create  the file attachment for this e-mail message.
                Attachment data = new Attachment(stream, Settings.CompanyName + "Invoice" + header.DocumentIdentifier + ".html", "text/html");
                // Add the file attachment to this e-mail message.
                message.Attachments.Add(data);

                //Send the message.
                SmtpClient client = new SmtpClient(Settings.MailServer);
                // Add credentials if the SMTP server requires them.
                client.Credentials = new NetworkCredential(Settings.MailUserName, Settings.MailPassword);
                client.Port        = Settings.MailPort;
                client.EnableSsl   = Settings.MailSSL;
                client.Send(message);
            }
            return(new AjaxReturn()
            {
                message = "Email sent to " + customer.Email
            });
        }
Beispiel #4
0
 public AjaxReturn DetailSave(NameAddress json)
 {
     checkNameType(json.Type, NameType);
     return(SaveRecord(json, true));
 }
 public AjaxReturn DetailPost(NameAddress json)
 {
     checkNameType(json.Type, NameType);
     return PostRecord(json, true);
 }
Beispiel #6
0
 public AjaxReturn NameSave(NameAddress json)
 {
     checkNameType(json.Type, "O");
     return(SaveRecord(json, true));
 }
 public AjaxReturn NamePost(NameAddress json)
 {
     checkNameType(json.Type, "O");
     return PostRecord(json, true);
 }
Beispiel #8
0
 /// <summary>
 /// Update a matched transaction
 /// </summary>
 public void StatementMatch()
 {
     Utils.Check(SessionData.StatementMatch != null, "Invalid call to StatementMatch");
     MatchInfo match = SessionData.StatementMatch.ToObject<MatchInfo>();
     Account account = Database.Get<Account>(match.id);
     // The existing transaction to match (or empty record if none)
     Extended_Document transaction = match.transaction < 0 ? Database.EmptyRecord<Extended_Document>() :
         SessionData.StatementImport.transactions[match.transaction].ToObject<Extended_Document>();
     // The statement transaction
     dynamic current = SessionData.StatementImport.import[match.current];
     Utils.Check(current != null, "No current transaction");
     bool same = match.type == "Same";
     bool documentHasVat = false;
     bool payment = false;
     decimal cAmount = current.Amount;
     int id = transaction.idDocument ?? 0;
     DocType type = match.transaction < 0 ?
         match.type == "Transfer" ?
             DocType.Transfer :		// They specified a new transfer
             cAmount < 0 ?			// They specified a new transaction - type depends on sign and account type
                 account.AccountTypeId == (int)AcctType.Bank ?
                     DocType.Cheque : DocType.CreditCardCharge :
                 account.AccountTypeId == (int)AcctType.Bank ?
                     DocType.Deposit : DocType.CreditCardCredit :
         (DocType)transaction.DocumentTypeId;
     GetParameters["acct"] = match.id.ToString();	// This bank account
     // Call appropriate method to get Record, and therefore transaction
     // Also set Module and Method, so appropriate template is used to display transaction before posting
     switch (type) {
         case DocType.Payment:
             Module = "customer";
             Method = "payment";
             Customer cust = new Customer();
             cust.Payment(id);
             this.Record = cust.Record;
             payment = true;
             break;
         case DocType.BillPayment:
             Module = "supplier";
             Method = "payment";
             Supplier supp = new Supplier();
             supp.Payment(id);
             this.Record = supp.Record;
             payment = true;
             break;
         case DocType.Cheque:
         case DocType.Deposit:
         case DocType.CreditCardCharge:
         case DocType.CreditCardCredit:
             Method = "document";
             Document(id, type);
             documentHasVat = true;
             break;
         case DocType.Transfer:
             Method = "transfer";
             Transfer(id);
             break;
         default:
             throw new CheckException("Unexpected document type:{0}", type.UnCamel());
     }
     dynamic record = (JObject)Record;
     dynamic doc = record.header;
     if (id == 0 && type == DocType.Transfer && cAmount > 0) {
         // New transfer in
         doc.TransferAccountId = match.id;
         doc.DocumentAccountId = 0;
         doc.DocumentAccountName = "";
     }
     if (string.IsNullOrWhiteSpace(doc.DocumentMemo.ToString())) {
         // Generate a memo
         string name = current.Name;
         string memo = current.Memo;
         if (string.IsNullOrWhiteSpace(memo))
             memo = name;
         else if (!memo.Contains(name))
             memo = name + " " + memo;
         doc.DocumentMemo = memo;
     }
     if (!same) {
         // They want to create a new document - try to guess the DocumentName
         string name = doc.DocumentName;
         string currentName = current.Name;
         currentName = currentName.Split('\n', '\t')[0];
         if (string.IsNullOrWhiteSpace(name) || (!payment && name.SimilarTo(currentName) < 0.5)) {
             doc.DocumentName = currentName;
             NameAddress n = new NameAddress() {
                 Type = "O",
                 Name = currentName
             };
             doc.DocumentNameAddressId = Database.Get(n).idNameAddress ?? 0;
         }
     }
     doc.DocumentDate = current.Date;
     decimal tAmount = doc.DocumentAmount;
     decimal diff = Math.Abs(cAmount) - Math.Abs(tAmount);
     doc.DocumentAmount += diff;
     if(same)
         Utils.Check(diff == 0, "Amounts must be the same");
     else {
         // New transaction
         doc.DocumentOutstanding = doc.DocumentAmount;
         doc.Clr = "";
         doc.idDocument = doc.Id = null;
         if (Utils.ExtractNumber(doc.DocumentIdentifier.ToString()) > 0)
             doc.DocumentIdentifier = "";
     }
     if(string.IsNullOrEmpty(doc.DocumentIdentifier.ToString())) {
         if (current.Id != null) {
             doc.DocumentIdentifier = current.Id;
         } else {
             int no = Utils.ExtractNumber(current.Name.ToString());
             if (no != 0)
                 doc.DocumentIdentifier = no.ToString();
         }
     }
     if (diff != 0 && documentHasVat) {
         // Adjust first line to account for difference
         if (record.detail.Count == 0)
             record.detail.Add(new InvoiceLine().ToJToken());
         dynamic line = record.detail[0];
         decimal val = line.LineAmount + line.VatAmount + diff;
         if (line.VatRate != 0) {
             line.VatAmount = Math.Round(val * line.VatRate / (100 + line.VatRate), 2);
             val -= line.VatAmount;
         }
         line.LineAmount = val;
     }
     if (payment && !same)
         removePayments(record);
     record.StatementAccount = match.id;
     if (same) {
         // Just post the new information
         if (type == DocType.Transfer)
             record = record.header;		// Transfer posts header alone
         AjaxReturn p = StatementMatchPost((JObject)record);
         if (p.error == null)
             Redirect(p.redirect);		// If no error, go on with matching
     }
 }